package lottery;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

/**
 * 娱乐彩票系统
 * @author qiujun
 *
 */
public class Lottery {

    int [] numbers = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32}; //号码
    int [][] optionalNos = new int [5][5];	//自选的号码组
    int [][] machineSelectionNos = new int[5][5]; //机选号码组
    int [] awardNo = new int[5]; //开奖号码
    String[] winningNos1 = new String[5]; //一等奖
    String[] winningNos2 = new String[5]; //二等奖
    String[] winningNos3 = new String[5]; //三等奖
    Scanner scanner = new Scanner(System.in); //接收键盘输入

    public void start() {
        System.out.println("***********************************************************");
        System.out.println("                     欢迎进入娱乐彩票系统");
        System.out.println("              从1~32中选出5个数字组成一组号码");
        System.out.println("***********************************************************");
        System.out.println("请输入编号！\n1. 机选 \n2.自选");
//        Arrays.sort(numbers);//排序
        int choise = scanner.nextInt();
        switch (choise) {
            case 1:
                machineSelection();
                break;
            case 2:
                optional();
                break;
            default:
                System.out.println("输入错误");
                break;
        }
        drawLottery();
        theLottery();
    }

    /**
     * 自选号码
     */
    public void optional() {
        String noStr = null;
        String [] noStrs = null;
        int optionNoIndex = 0;
        while (true) {
            System.out.println("请输入号码格式为：\"数字,数字,数字,数字,数字\"。");
            noStr = scanner.next();
            noStrs = noStr.split(",");//按逗号进行分割，返回字符串数组
            if(noStrs.length > awardNo.length) {
                System.out.println("最多只能输入" + awardNo.length + "个数，请重新输入！");
                continue;
            }
            //输入的数字不能大于32
            for (String string : noStrs) {
                //把字符串转换成整数
                if(Integer.parseInt(string) > numbers[numbers.length - 1] || Integer.parseInt(string) < numbers[0]) {
                    System.out.println("号码不能大于"+ numbers[numbers.length - 1]+"或小于"+numbers[0]+"，请从新输入！");
                    break;
                }
            }
            if(isSameElement(noStrs)) {
                System.out.println("不能有相同数组，请从新输入！");
                continue;
            }
            int[] no = new int[noStrs.length];
            for (int j = 0; j < no.length; j++) {
                no[j] = Integer.parseInt(noStrs[j]);//字符串转整数
            }
            Arrays.sort(no);
            optionalNos[optionNoIndex] = no;
            optionNoIndex++;
            if(optionNoIndex == optionalNos.length) {
                System.out.println("选好完毕!");
                break;
            }
            System.out.println("是否继续选号码？Y/N");
            if(!"y".equalsIgnoreCase(scanner.next())) {
                break;
            }
        }
        System.out.println("是否需要机选5注，y/n");
        if("y".equals(scanner.next())) {
            machineSelection();
        }
    }

    /**
     * 判断String数组中是否有相同元素
     * @param strs 要判断的String数组
     * @return 有相同元素返回true，没有返回false
     */
    public boolean isSameElement(String [] strs) {
        for (int i = 0; i < strs.length; i++) {
            for (int j = 0; j < strs.length; j++) {
                if(j < strs.length -1 && i != j + 1  && strs[i].equals(strs[j+1]) ) {
                    return true;
                }
            }
        }
        return false;
    }
    /**
     * 机选号码
     */
    public void machineSelection() {
        Random random = new Random();
        for (int i = 0; i < machineSelectionNos.length; i++) {
            for (int j = 0; j < machineSelectionNos[i].length; j++) {
                //产生1-32的随机数
                int value = random.nextInt(numbers.length)+1;
                //判断数组中是否已包含value，如果包含就不能用该数字，则从新产生随机数
                while (true) {
                    if(container(machineSelectionNos[i], value)) {
                        value = random.nextInt(numbers.length);
                    }else {
                        break;
                    }
                }
                machineSelectionNos[i][j] = value;
            }
            Arrays.sort(machineSelectionNos[i]);
        }
    }

    /**
     * 摇奖
     */
    public void drawLottery() {
        Random random = new Random();
        for (int i = 0; i < awardNo.length; i++) {
            //产生1-32的随机数
            int value = random.nextInt(numbers.length)+1;
            //判断数组中是否已包含value，如果包含就不能用该数字，则从新产生随机数
            while (true) {
                if(container(awardNo, value)) {
                    value = random.nextInt(numbers.length);
                }else {
                    break;
                }
            }
            awardNo[i] = value;
        }
        //数组排序方法
        Arrays.sort(awardNo);
    }

    /**
     * 判断数组arr中是否包含整数a
     * @param arr int数组
     * @param a int整数
     * @return 包含返回true反之返回false
     */
    public boolean container(int[] arr, int a) {
        boolean flag = false;
        for (int i : arr) {
            if(i == a) {
                flag = true;
                break;
            }
        }
        return flag;
    }
    /**
     * 开奖
     */
    public void theLottery() {
        //找出机选中奖号码
        if(machineSelectionNos[0][1] != 0) {
            for (int i = 0; i < machineSelectionNos.length; i++) {
                int count = 0;//count等3，代表中了3个号码为3等奖，等于4，代表中了4个号码为2等奖，等于5，代表中了5个号码为1等奖
                for (int j = 0; j < awardNo.length; j++) {

                    if(container(machineSelectionNos[i],awardNo[j])) {
                        count++;
                    } else {
                        continue;
                    }
                }
                //根据1，2，3等奖分别把号码放入其中
                switch (count) {
                    case 3:
                        winningNos3[i] = Arrays.toString(machineSelectionNos[i]);//把数组转换成一个字符串  "1, 9, 16, 27, 30"
                        break;
                    case 4:
                        winningNos2[i] = Arrays.toString(machineSelectionNos[i]);
                        break;
                    case 5:
                        winningNos1[i] = Arrays.toString(machineSelectionNos[i]);
                        break;
                    default:
                        break;
                }
            }
        }
        //找出自选中奖号码
        if(optionalNos[0][1] != 0) {
            for (int i = 0; i < optionalNos.length; i++) {
                int count = 0;//count等1代表中了3个号码为3等奖，等于2代表中了4个号码为2等奖，等于3代表中了5个号码为1等奖
                for (int j = 0; j < awardNo.length; j++) {

                    if(container(optionalNos[i],awardNo[j])) {
                        count++;
                    } else {
                        continue;
                    }
                }
                //根据1，2，3等奖分别把号码放入其中
                switch (count) {
                    case 3:
                        winningNos3[i] = Arrays.toString(optionalNos[i]);
                        break;
                    case 4:
                        winningNos2[i] = Arrays.toString(optionalNos[i]);
                        break;
                    case 5:
                        winningNos1[i] = Arrays.toString(optionalNos[i]);
                        break;

                    default:
                        break;
                }
            }
        }

        System.out.println("自选号码为：");
        for (int i = 0; i < optionalNos.length; i++) {
            System.out.println(Arrays.toString(optionalNos[i]));
        }
        System.out.println("机选号码为：");
        for (int i = 0; i < machineSelectionNos.length; i++) {
            System.out.println(Arrays.toString(machineSelectionNos[i]));
        }
        System.out.println("开奖号码为：");
        System.out.println(Arrays.toString(awardNo));
        System.out.println("1等奖中奖号码为：");
        System.out.println(Arrays.toString(winningNos1));
        System.out.println("2等奖中奖号码为：");
        System.out.println(Arrays.toString(winningNos2));
        System.out.println("3等奖中奖号码为：");
        System.out.println(Arrays.toString(winningNos3));
    }
    public void clean() {
        Arrays.fill(winningNos1, null);
        Arrays.fill(winningNos2, null);
        Arrays.fill(winningNos3, null);
    }

    public static void main(String[] args) {
        Lottery lottery = new Lottery();
        do {
            lottery.start();
            System.out.println("是否继续?Y/N");
            //清空1、2、3等奖数组
            lottery.clean();
        } while ("Y".equalsIgnoreCase(lottery.scanner.next()));
    }
}
